//
//  KDDetailNotyController.m
//  WangJingApp
//
//  Created by 魏强 on 17/2/10.
//  Copyright © 2017年 KDTech. All rights reserved.
//  通知详情页

#import "KDDetailNotyController.h"
#import "WQSeparatorView.h"
#import "KDCommentCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "UIButton+WQExtension.h"
@interface KDDetailNotyController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *commentTableView;
/**  toolbarde底部View */
@property (weak, nonatomic) IBOutlet WQSeparatorView *toolView;
/**  底部toolBar */
@property (weak, nonatomic) IBOutlet UIView *toolBar;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scroll_cons_w;
/**  评论汇总 */
@property (nonatomic, strong) NSMutableArray *infoArr;
/**  段头 */
@property (nonatomic, strong) UIView *header;
/**  用户头像Btn */
@property (weak, nonatomic) IBOutlet UIButton *userAvatarBtn;

@end

@implementation KDDetailNotyController
#pragma mark - 懒加载区

#pragma mark - 生命周期区
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.scroll_cons_w.constant = screenW;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.toolBar.separatorMode = WQViewSeparatorModeAll;
    [self setupHeader];
    [self setupToolbar];
    [self setupTableView];
    
}

#pragma mark - 板块布置区
- (void)setupHeader{
    
    [self.userAvatarBtn setHeaderWithUrl:@"http://tva4.sinaimg.cn/crop.0.24.149.149.50/006892xejw8f3hzcquzzrj304505k3yd.jpg" forState:UIControlStateNormal placeholderStr:nil];
//    [self.userAvatarBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
}
- (void)setupTableView{
    self.commentTableView.layer.cornerRadius = 5;
    self.commentTableView.layer.borderWidth = 1;
    self.commentTableView.layer.borderColor = WQColor(244, 244, 244).CGColor;
    self.commentTableView.clipsToBounds = YES;
    self.commentTableView.autoresizingMask &= ~UIViewAutoresizingFlexibleBottomMargin;
    NSString *cellId = @"KDCommentCell";
    UINib *xib = [UINib nibWithNibName:cellId bundle:nil];
    
    [self.commentTableView registerNib:xib forCellReuseIdentifier:cellId];
    //    self.commentTableView.separatorColor = WQColor(245, 245, 245);
    self.commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.commentTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    self.commentTableView.footer = footer;
    [self.commentTableView.header beginRefreshing];
}
- (void)setupToolbar{
    self.toolBar.layer.cornerRadius = 5;
    self.toolBar.layer.borderWidth = 1;
    self.toolBar.layer.borderColor = WQColor(244, 244, 244).CGColor;
    self.toolBar.clipsToBounds = YES;
    self.toolView.separatorMode = WQViewSeparatorModeTop;
    self.toolView.separatorColor = WQColor(244, 244, 244);
}
#pragma mark - 事件点击区
- (void)comentNumBtnClick{
    
}
#pragma mark - 网络请求区
- (NSMutableArray *)getFakeData
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"FakeSubComment" ofType:@"plist"];
    
    //    NSArray *data = [[NSArray alloc] initWithContentsOfFile:plistPath];
    return [KDComment objectArrayWithFile:plistPath];
    
}
- (void)loadData{
    self.infoArr = [self getFakeData];
    [self.commentTableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.commentTableView.header endRefreshing];
    });
}
- (void)loadMoreData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.commentTableView.footer endRefreshing];
    });
}
#pragma mark - 通知接收区

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KDCommentCell *cell = [KDCommentCell cellWithTableView:tableView];
    cell.model = self.infoArr[indexPath.row];
//    cell.delegate = self;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (!self.header) {
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 44)];
    header.backgroundColor = [UIColor whiteColor];
    self.header = header;
    UILabel * lb1 = [self createLabelWithTitle:@"共" frame:CGRectMake(10, 0, 0, self.header.height)];
    UIButton *comentNumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        KDLog(@"self.infoArr.count:%ld", self.infoArr.count);
    NSString *num = [NSString stringWithFormat:@"%ld", self.infoArr.count*10 ];
    [comentNumBtn setTitle:num forState:UIControlStateNormal];
     comentNumBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    CGSize titleSize = [num sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:comentNumBtn.titleLabel.font.fontName size:13]}];
    comentNumBtn.frame = CGRectMake(CGRectGetMaxX(lb1.frame), 0, titleSize.width+5, 15);

   
    [comentNumBtn addTarget:self action:@selector(comentNumBtnClick) forControlEvents:UIControlEventTouchUpInside];
    comentNumBtn.backgroundColor = [UIColor redColor];
    comentNumBtn.layer.cornerRadius = 5;
    comentNumBtn.clipsToBounds = YES;
    [comentNumBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];;
    [self.header addSubview:comentNumBtn];
    comentNumBtn.centerY = header.centerY;
    
    [self createLabelWithTitle:@"条回复" frame:CGRectMake(CGRectGetMaxX(comentNumBtn.frame), 0, 0, self.header.height)];
//    }
    
    return self.header;
}
- (UILabel *)createLabelWithTitle:(NSString *)title frame:(CGRect)frame{
    UILabel *labelName = [[UILabel alloc]init];
    labelName.frame = frame;
    labelName.text = title;
    [labelName sizeToFit];
    labelName.centerY = self.header.centerY;
    [self.header addSubview:labelName];
    return labelName;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"KDCommentCell" configuration:^(KDCommentCell * cell) {
        // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:"
        // Like:
        cell.model = self.infoArr[indexPath.row];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
